package com.ys.controller.web.storage;

import com.alibaba.fastjson.JSON;
import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.base.TbPlatform;
import com.ys.entity.storage.TccLogisticsServiceProvider;
import com.ys.entity.sys.YsMenuNo;
import com.ys.mapper.sys.YsMenuNoMapper;
import com.ys.service.flow.YspTaskService;
import com.ys.service.storage.TccLogisticsServiceProviderService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.AESUtil;
import com.ys.util.DateUtil;
import com.ys.vo.Flow;
import com.ys.vo.W;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/web/storage/tccLogisticsServiceProvider")
public class WebTccLogisticsServiceProviderController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccLogisticsServiceProviderController.class);

	@Autowired
	private CommonApiService<TccLogisticsServiceProvider> commonApiService;

	@Autowired
	private TccLogisticsServiceProviderService tccLogisticsServiceProviderService;

	@Resource
	private YsMenuNoMapper ysMenuNoMapper;

	//菜单id
	private Long menuId = MenuId.TCC_LogisticsServiceProvider;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
		List<Map<String, Object>> sonlist = new ArrayList<>();
		try {
			String providerBid = "";
			YsMenuNo entity = new YsMenuNo().setMenuId(MenuId.TCC_LogisticsServiceProvider).queryFirst();
			if (entity == null) {
				throw new Exception("尚未设置单号格式");
			} else {
				Integer currentNo = entity.getCurrentNo();
				String connectCode = entity.getConnectCode();
				String preCode = entity.getPreCode();
				// 序号+1
				entity.setCurrentNo(currentNo + 1);
				ysMenuNoMapper.update(entity);
				String currentNoNew = String.format("%04d",currentNo);
				providerBid =  preCode + connectCode + currentNoNew;
			}
			TccLogisticsServiceProvider tccLogisticsServiceProvider = new TccLogisticsServiceProvider().queryFirst();
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_LogisticsServiceProvider, sonlist);
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("status", 1);
//			extraParam.put("rateCode", tpAmazonShopExchangeRate.getCode());
			return commonApiService.toAdd(model, new TccLogisticsServiceProvider().setProviderBid(providerBid).setStatus(1), MenuId.TCC_LogisticsServiceProvider, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params,Long id){
		try {
		if (params.get("id") == null){
			return W.error(model, "id不能为空");
		}
		TccLogisticsServiceProvider tccLogisticsServiceProvider = new TccLogisticsServiceProvider().setId(id).queryFirst();

			//子表信息
		List<Map<String, Object>> itemList = tccLogisticsServiceProviderService.getItemList(id);
		// 子列表配置信息
		SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

		Map<String, Object> extraParam = new TccLogisticsServiceProvider().setId(id)
				.addLeftJoin("TCC_LogisticsServiceProviderItem C ON C.Pid=A.id")
				.addLeftJoin("TB_CompanyInfo B on  B.Vname = C.BankAccountName")
				.addField("B.Vname bankAccountName")
				.queryFirstMap();

		extraParam.put("factno", AESUtil.aesDecrypt(tccLogisticsServiceProvider.getFactno()));
		extraParam.put("supno", AESUtil.aesDecrypt(tccLogisticsServiceProvider.getSupno()));
		extraParam.put("suppass", AESUtil.aesDecrypt(tccLogisticsServiceProvider.getSuppass()));
		extraParam.put("appkey", AESUtil.aesDecrypt(tccLogisticsServiceProvider.getAppkey()));

			if (extraParam == null){
			return W.error(model, "该物流服务商信息不存在!");
		}


			return commonApiService.toEdit(model, id, new TccLogisticsServiceProvider().setId(id), MenuId.TCC_LogisticsServiceProvider, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}